package com.cloudm.technician.ui.modules.cloudm_box.change_info

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseFragment
import com.cloudm.technician.ui.modules.cloudm_box.debug_result.DebugResultActivity
import kotlinx.android.synthetic.main.fragment_change_info.*

/**
 * 作者: wesley
 * 时间: 2017/12/25
 * 描述: ui
 */
class ChangeInfoFragment : BaseFragment<ChangeInfoPresenter>(), ChangeInfoContract.View {

    private var info: DeviceInfo? = null
    private var sn: String? = null

    override val resource: Int
        get() = R.layout.fragment_change_info

    companion object {
        fun newInstance(info: DeviceInfo?, sn: String?) = ChangeInfoFragment().apply {
            arguments = Bundle().apply {
                putParcelable("info", info)
                putString("sn", sn)
            }
        }
    }

    override fun initInject() {
        DaggerChangeInfoComponent.builder().appComponent(appComponent).changeInfoPresenterModule(
                ChangeInfoPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable("info")
        sn = bundle.getString("sn")
    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {
        tvSnCodeValue.text = sn
        tvSnCodeValueOld.text = info?.sn
    }

    override fun initListener() {

    }

    override fun changeBoxFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }

    override fun showDeviceInfoEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.device_info_is_null))
    }

    override fun changeBoxSuccess(info: DeviceInfo) {
        val intent = Intent(mContext, DebugResultActivity::class.java).apply {
            putExtras(Bundle().apply {
                putParcelable("info", info)
            })
        }
        mContext.openActivity(intent)
    }

    override fun showChangeBoxDialog() {
        showLoadingDialog(mContext.resources.getString(R.string.change_boxing))
    }

    override fun hideChangeBoxDialog() {
        hideLoadingDialog()
    }

    fun nestStep() {
        presenter?.changeCloudBox(info?.id ?: -1, sn ?: "-1")
    }
}